*************************************************
* Purpose -- Event study analysis with CDLZ 
* events and CDLZ method
************************************************
clear all
* RUN 00_path_master.do FIRST TO GET FILEPATHS


*************************************************
* Purpose -- Load and clean CDLZ sample
*************************************************

frame create cdlz_events
frame change cdlz_events

use "$raw/other/CDLZevents.dta", clear

gen largetreat = alltreat == 1 & fedinc == 0 & toosmall == 0
gen smalltreat = alltreat == 1 & fedinc == 0 & toosmall == 1
gen statetreat = alltreat == 1 & fedinc == 0

collapse (max) largetreat smalltreat statetreat, by(statenum year)

rename statenum statefips

drop if inlist(statefips,2,11,15)

keep if year>= 1987

tempfile cdlz_events
save `cdlz_events'


*************************************************
* Load MSCZ and BCP samples and merge events
*************************************************

* 1. MSCZ

frame create msczes_cdlz
frame change msczes_cdlz

use "$clean/qcew/mscz_sample.dta", clear


* 2. BCP

frame create bcpes_cdlz
frame change bcpes_cdlz

use "$clean/qcew/bcp_sample.dta", clear

frame copy bcpes_cdlz bcpes_drbs


************************************************
* Constructing events, post-periods, clean controls
* for CDLZ events
************************************************

local j = 1
foreach f in msczes_cdlz bcpes_cdlz {
	if `j' == 1 {
		local unit = "pair_id_czstate"
	}
	
	if `j' == 2 {
		local unit = "pair_id_county"
	}
	
	frame change `f'
	
	forvalues i = 1987/1989 {
		insobs 1
		replace year = `i' if _n == _N
	}

	fillin `unit' year
	drop if mi(`unit')
	bys `unit' (year): replace statefips = statefips[_N] if mi(statefips)
	assert ~mi(statefips)
	drop _fillin
	
	merge m:1 statefips year using `cdlz_events', keep(1 3) nogen
	
	xtset `unit' year
	

	
	gen lepop75 = ln(annual_avg_emplvl75/pop)
	
	* creating pre and post-periods
	
	foreach var in learn75 lepop75 {
		gen `var'_pre = L.`var'
		gen `var'_post = (`var' + F.`var' + F2.`var' + F3.`var' + F4.`var' + F5.`var')/6
		replace `var'_post = (`var' + F.`var' + F2.`var' + F3.`var' + F4.`var')/5 if year == 2015
		replace `var'_post = (`var' + F.`var' + F2.`var' + F3.`var')/4 if year == 2016
		
		gen `var'_dif = `var'_post - `var'_pre
	}
	
	gen max_lmw_dif = F5.lmw - L.lmw
	replace max_lmw_dif = F4.lmw - L.lmw if year == 2015
	replace max_lmw_dif = F3.lmw - L.lmw if year == 2016
	
	* creating smalltreat and largetreat for 2017-2019
	
	gen mw_increase = mw > L.mw & mw > fed_mw if year>= 2017
	replace largetreat = 1 if mw >= 1.05*L.mw & mw >= L.mw + 0.25 & mw > fed_mw & year>= 2017
	replace largetreat = 0 if year>= 2017 & mi(largetreat)
	replace smalltreat = 1 if mw_increase == 1 & largetreat == 0 & year>= 2017
	replace smalltreat = 0 if year>= 2017 & mi(smalltreat)
	replace statetreat = 0 if year>= 2017
	replace statetreat = 1 if year>= 2017 & (smalltreat == 1 | largetreat == 1)
	*/
	
	* creating some new variables as controls
	
	gen fed_increase = inlist(year,1990,1991,1996,1997,2007,2008,2009) & largetreat == 1
	
	gen postfed = (fed_increase == 1 | F.fed_increase == 1 | F2.fed_increase == 1 | ///
	F3.fed_increase == 1 | F4.fed_increase == 1 | F5.fed_increase == 1) & largetreat == 1
	gen postsmall = (F.smalltreat == 1 | F2.smalltreat == 1 | ///
	F3.smalltreat == 1 | F4.smalltreat == 1 | F5.smalltreat == 1) & largetreat == 1
	
	gen prefed =  (L.fed_increase == 1 | L2.fed_increase == 1 | L3.fed_increase == 1) & largetreat == 1
	gen presmall = (L.smalltreat == 1 | L2.smalltreat == 1 | L3.smalltreat == 1) & largetreat == 1
	gen pretreat = (L.largetreat == 1 | L2.largetreat == 1 | L3.largetreat == 1) & largetreat == 1
	
	* identifying clean control areas
	
	gen clean_control = 0
	
	replace clean_control = 1 if L.statetreat == 0 & L2.statetreat == 0 & ///
	L3.statetreat == 0 & statetreat == 0 & F.statetreat == 0 & ///
	F2.statetreat == 0 & F3.statetreat == 0 & F4.statetreat == 0 & F5.statetreat == 0 & inrange(year,1990,2014)
	
	replace clean_control = 1 if L.statetreat == 0 & L2.statetreat == 0 & ///
	L3.statetreat == 0 & statetreat == 0 & F.statetreat == 0 & ///
	F2.statetreat == 0 & F3.statetreat == 0 & year == 2016
	
	replace clean_control = 1 if L.statetreat == 0 & L2.statetreat == 0 & ///
	L3.statetreat == 0 & statetreat == 0 & F.statetreat == 0 & ///
	F2.statetreat == 0 & F3.statetreat == 0 & F4.statetreat == 0 & year == 2015

	
	keep if inrange(year,1990,2016)

	local ++j
}


************************************************
* Running regressions
************************************************

local f = 1
foreach frame in msczes_cdlz bcpes_cdlz {
	
	local controls "prefed postfed presmall pretreat postsmall"
	
	frame `frame' {
		
		
		eststo `frame'_earn_uw: reghdfe learn75_dif largetreat `controls' if ///
		(largetreat == 1 | clean_control == 1), a(pair_id_num##year) cluster(statefips)
		eststo `frame'_emp_uw: reghdfe lepop75_dif largetreat `controls' if ///
		(largetreat == 1 | clean_control == 1), a(pair_id_num##year) cluster(statefips)
		eststo `frame'_mwe_uw: ivreghdfe lepop75_dif (max_lmw_dif = largetreat) `controls' ///
		if (largetreat == 1 | clean_control == 1), a(pair_id_num##year) cluster(statefips)
		eststo `frame'_owe_uw: ivreghdfe lepop75_dif (learn75_dif = largetreat) `controls' ///
		if (largetreat == 1 | clean_control == 1), a(pair_id_num##year) cluster(statefips)
		
		eststo `frame'_earn_w: reghdfe learn75_dif largetreat `controls' if ///
		(largetreat == 1 | clean_control == 1) [aw=pop], a(pair_id_num##year) cluster(statefips)
		eststo `frame'_emp_w: reghdfe lepop75_dif largetreat `controls' if ///
		(largetreat == 1 | clean_control == 1) [aw=pop], a(pair_id_num##year) cluster(statefips)
		eststo `frame'_mwe_w: ivreghdfe lepop75_dif (max_lmw_dif = largetreat) `controls' ///
		if (largetreat == 1 | clean_control == 1) [aw=pop], a(pair_id_num##year) cluster(statefips)
		eststo `frame'_owe_w: ivreghdfe lepop75_dif (learn75_dif = largetreat) `controls' ///
		if (largetreat == 1 | clean_control == 1) [aw=pop], a(pair_id_num##year) cluster(statefips)
		
		gen flag = e(sample)
		preserve
			keep if flag == 1
			duplicates drop statefips year, force
			count if largetreat == 1
			local events = r(N)
		restore
		estadd local num_events `events'
	}
	
	local ++f
}


estimates dir
foreach e in `r(names)' {
	estimates restore `e'
	estimates title: `e'
	estimates save "$intermediate/es_cdlz_estimates", append 
}